package com.powernode.config;

import com.powernode.constant.OrderConstant;
import com.powernode.constant.QueueConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建延迟队列 设置延迟队列过期时间
 * 延迟队列过期消息的路由配置
 * 创建死信队列  创建死信交换机 完成队列与交换机绑定
 */
@Configuration
public class OrderQueueConfig {

    /**
     * 延迟队列
     */
    @Bean
    public Queue msQueue() {
        Map<String, Object> args = new HashMap<>();
        //消息死后到哪个交换机去
        args.put("x-dead-letter-exchange", QueueConstant.ORDER_DEAD_EX);
        //消息死后到哪个路由key
        args.put("x-dead-letter-routing-key", QueueConstant.ORDER_DEAD_KEY);
        //消息多久后死亡
        args.put("x-message-ttl", 15 * 60 * 1000);
        return new Queue(QueueConstant.ORDER_MS_QUEUE, true, false, false, args);
    }

    /**
     * 死信
     *
     * @return
     */
    @Bean
    public Queue deadQueue() {
        //return new Queue(QueueConstant.ORDER_DEAD_QUEUE);
        return QueueBuilder.durable(QueueConstant.ORDER_DEAD_QUEUE).build();
    }

    @Bean
    public DirectExchange deadEx() {
        return new DirectExchange(QueueConstant.ORDER_DEAD_EX);
        // return ExchangeBuilder.fanoutExchange("").durable();
    }

    @Bean
    public Binding deadBind() {
        return BindingBuilder.bind(deadQueue()).to(deadEx()).with(QueueConstant.ORDER_DEAD_KEY);
    }

    /**
     * es队列
     *
     * @return
     */
    @Bean
    public Queue esChangeQueue() {
        //return new Queue(QueueConstant.ORDER_DEAD_QUEUE);
        return QueueBuilder.durable(QueueConstant.ES_CHANGE_QUEUE).build();
    }

    /**
     * msg队列
     *
     * @return
     */
    @Bean
    public Queue wxMsgQueue() {
        //return new Queue(QueueConstant.ORDER_DEAD_QUEUE);
        return QueueBuilder.durable(QueueConstant.WX_MSG_QUEUE).build();
    }


}